{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 百度智能春联\n",
    "百度智能春联:根据用户输入的命题关键词自动生成一副春联，包括上联、下联和横批。\n",
    "\n",
    "百度AI智能春联产品介绍链接:https://ai.baidu.com/tech/intelligentwriting\n",
    "\n",
    "百度AI智能春联python接口使用说明:https://ai.baidu.com/ai-doc/NLP/Ok53wb6dh"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 开始我们的实验\n",
    "## (1)登录百度AI开发平台，选择“智能春联”，使用“创建应用”建立一个智能春联应用。\n",
    "获取应用的AppID、API Key、Secret Key，注意:Secret Key要先选择显示，然后复制其现实的字符。\n",
    "\n",
    "例如我创建的智能春联应用获取的AppID、API Key、Secret Key如下：\n",
    "\n",
    "    #公开课百度AI春联写作应用的id、key、secret，必须更改为自己的api接口\n",
    "    baidu_app_id = '20652704'\n",
    "    baidu_api_key = 'Q7TK78cRwG7jCsGc4njYIHhi'\n",
    "    baidu_api_secret = '8wunyudqeDgjQq9ZTpBjL3sIpOiMUYRG'\n",
    "\n",
    "注：对于“智能春联”功能来说，本步必须的，因为百度AI开发平台对于“智能春联”中普通话识别，只提供100次调用试用。您使用的课程时，肯定已经超过100次了。\n",
    "\n",
    "百度AI开发平台注册链接:https://login.bce.baidu.com/?account=&redirect=http%3A%2F%2Fconsole.bce.baidu.com%2F%3Ffromai%3D1#/aip/overview\n",
    "\n",
    "## (2)熟悉我封装的百度AI智能春联的模块\n",
    "在代码中输入如下代码，导入我的封装的模块\n",
    "\n",
    "    import sys\n",
    "    sys.path.append('../baidu_api_lib')\n",
    "    from baidu_couplets import baidu_couplets_create\n",
    "封装库提供两个方法：初始化方法，获取春联方法。\n",
    "\n",
    "### <1>初始化方法，需要传入三个参数，就是上步我们创建“智能春联”应用获取的三个参数\n",
    "参数信息如下：\n",
    "\n",
    "    #公开课百度AI春联写作应用的id、key、secret，必须更改为自己的api接口\n",
    "    baidu_app_id = '20652704'\n",
    "    baidu_api_key = 'Q7TK78cRwG7jCsGc4njYIHhi'\n",
    "    baidu_api_secret = '8wunyudqeDgjQq9ZTpBjL3sIpOiMUYRG'\n",
    "\n",
    "调用方式：\n",
    "\n",
    "    #传入百度AI的参数给智能春联创作\n",
    "    couplets = baidu_couplets_create(baidu_app_id, baidu_api_key, baidu_api_secret)\n",
    "\n",
    "初始化完毕后，获取到couplets对象，操作该对象就能进行智能春联创作了。\n",
    "\n",
    "### <2>获取春联方法方法\n",
    "\n",
    "这是我封装百度语音识别方法的函数原型：\n",
    "\n",
    "    get_couplets(request_url, word): \n",
    "    \n",
    "    get_couplets包含2个参数，其中都为必须填写的内容。\n",
    "    request_url:百度智能春联api的连接，\n",
    "    baidu_request_url = 'https://aip.baidubce.com/rpc/2.0/creation/v1/couplets'。\n",
    "    word:春联的关键字，用双引号包裹起来"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "######################################################\n",
    "#\n",
    "# 先感受下小派写春联,选择本cell，按shirt+enter键运行本模块\n",
    "#\n",
    "######################################################\n",
    "#!/usr/bin/env python\n",
    "# -*- coding: utf-8 -*-\n",
    "#\n",
    "# Copyright (c) 2020 Taste all Pi.\n",
    "#\n",
    "# Licensed under the GNU General Public License, Version 2.0 (the \"License\");\n",
    "# you may not use this file except in compliance with the License.\n",
    "# You may obtain a copy of the License at\n",
    "#\n",
    "#   http://www.gnu.org/licenses/gpl-2.0.html\n",
    "#\n",
    "# Unless required by applicable law or agreed to in writing, software\n",
    "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "# See the License for the specific language governing permissions and\n",
    "# limitations under the License.\n",
    "\n",
    "#导入标准库\n",
    "import sys\n",
    "import os\n",
    "\n",
    "#导入自定义库\n",
    "sys.path.append('../baidu_api_lib')\n",
    "from baidu_couplets import baidu_couplets_create\n",
    "from baidu_sound import baidu_word_2_sound\n",
    "\n",
    "#公开课百度AI春联写作应用的id、key、secret，必须更改为自己的api接口\n",
    "baidu_app_id = '20652704'\n",
    "baidu_api_key = 'Q7TK78cRwG7jCsGc4njYIHhi'\n",
    "baidu_api_secret = '8wunyudqeDgjQq9ZTpBjL3sIpOiMUYRG'\n",
    "\n",
    "#百度AI对联应用api地址\n",
    "baidu_request_url = 'https://aip.baidubce.com/rpc/2.0/creation/v1/couplets'\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    try:\n",
    "        #传入百度AI的参数给智能春联创作\n",
    "        couplets = baidu_couplets_create(baidu_app_id, baidu_api_key, baidu_api_secret)\n",
    "        \n",
    "        #传入百度AI的参数给语音合成模块\n",
    "        word_2_sound = baidu_word_2_sound(baidu_app_id, baidu_api_key, baidu_api_secret)\n",
    "        \n",
    "        #获取春天为关键字的春联\n",
    "        word = couplets.get_couplets(baidu_request_url, \"幸福\")\n",
    "        \n",
    "        #打印获取的春联\n",
    "        print(word.json()['couplets']['first'])\n",
    "        print(word.json()['couplets']['second'])\n",
    "        print(word.json()['couplets']['center'])\n",
    "        \n",
    "        #进行语音合成上联\n",
    "        word_2_sound.trans_word_to_sound(\"上联是\" + word.json()['couplets']['first'],'tst_sound.wav')\n",
    "        os.system('mplayer ' + 'tst_sound.wav')\n",
    "        \n",
    "        #进行语音合成下联\n",
    "        word_2_sound.trans_word_to_sound(\"下联是\" + word.json()['couplets']['second'],'tst_sound.wav')\n",
    "        os.system('mplayer ' + 'tst_sound.wav')\n",
    "        \n",
    "        #进行语音合成横批\n",
    "        word_2_sound.trans_word_to_sound(\"横批是\" + word.json()['couplets']['center'],'tst_sound.wav')\n",
    "        os.system('mplayer ' + 'tst_sound.wav')\n",
    "        \n",
    "        #删除临时文件    \n",
    "        os.remove('tst_sound.wav')\n",
    "                       \n",
    "    except KeyboardInterrupt: \n",
    "        os.remove('tst_sound.wav')\n",
    "        print(\"任务被终止\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 让我们临摹代码，学习“智能写春联”功能"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# #导入标准库\n",
    "# import sys\n",
    "\n",
    "# import os\n",
    "\n",
    "\n",
    "# #导入自定义库\n",
    "# sys.path.append('../baidu_api_lib')\n",
    "\n",
    "# from baidu_couplets import baidu_couplets_create\n",
    "\n",
    "# from baidu_sound import baidu_word_2_sound\n",
    "\n",
    "\n",
    "# #公开课百度AI春联写作应用的id、key、secret，必须更改为自己的api接口\n",
    "# baidu_app_id = '20652704'\n",
    "\n",
    "# baidu_api_key = 'Q7TK78cRwG7jCsGc4njYIHhi'\n",
    "\n",
    "# baidu_api_secret = '8wunyudqeDgjQq9ZTpBjL3sIpOiMUYRG'\n",
    "\n",
    "\n",
    "\n",
    "# #百度AI对联应用api地址\n",
    "# baidu_request_url = 'https://aip.baidubce.com/rpc/2.0/creation/v1/couplets'\n",
    "\n",
    "\n",
    "# if __name__ == \"__main__\":\n",
    "\n",
    "#     try:\n",
    "\n",
    "#         #传入百度AI的参数给智能春联创作\n",
    "#         couplets = baidu_couplets_create(baidu_app_id, baidu_api_key, baidu_api_secret)\n",
    "\n",
    "        \n",
    "#         #传入百度AI的参数给语音合成模块\n",
    "#         word_2_sound = baidu_word_2_sound(baidu_app_id, baidu_api_key, baidu_api_secret)\n",
    "\n",
    "        \n",
    "#         #获取春天为关键字的春联\n",
    "#         word = couplets.get_couplets(baidu_request_url, \"春天\")\n",
    "\n",
    "        \n",
    "#         #打印获取的春联\n",
    "#         print(word.json()['couplets']['first'])\n",
    "\n",
    "#         print(word.json()['couplets']['second'])\n",
    "\n",
    "#         print(word.json()['couplets']['center'])\n",
    "\n",
    "        \n",
    "#         #进行语音合成上联\n",
    "#         word_2_sound.trans_word_to_sound(\"上联是\" + word.json()['couplets']['first'],'tst_sound.wav')\n",
    "\n",
    "#         os.system('mplayer ' + 'tst_sound.wav')\n",
    "\n",
    "        \n",
    "#         #进行语音合成下联\n",
    "#         word_2_sound.trans_word_to_sound(\"下联是\" + word.json()['couplets']['second'],'tst_sound.wav')\n",
    "\n",
    "#         os.system('mplayer ' + 'tst_sound.wav')\n",
    "\n",
    "        \n",
    "#         #进行语音合成横批\n",
    "#         word_2_sound.trans_word_to_sound(\"横批是\" + word.json()['couplets']['center'],'tst_sound.wav')\n",
    "\n",
    "#         os.system('mplayer ' + 'tst_sound.wav')\n",
    "\n",
    "        \n",
    "#         #删除临时文件    \n",
    "#         os.remove('tst_sound.wav')\n",
    "\n",
    "                       \n",
    "#     except KeyboardInterrupt: \n",
    "#         os.remove('tst_sound.wav')\n",
    "\n",
    "#         print(\"任务被终止\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 课后练习\n",
    "\n",
    "更改春联关键字，看看获取不同的春联"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#!/usr/bin/env python\n",
    "# -*- coding: utf-8 -*-\n",
    "#\n",
    "# Copyright (c) 2020 Taste all Pi.\n",
    "#\n",
    "# Licensed under the GNU General Public License, Version 2.0 (the \"License\");\n",
    "# you may not use this file except in compliance with the License.\n",
    "# You may obtain a copy of the License at\n",
    "#\n",
    "#   http://www.gnu.org/licenses/gpl-2.0.html\n",
    "#\n",
    "# Unless required by applicable law or agreed to in writing, software\n",
    "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "# See the License for the specific language governing permissions and\n",
    "# limitations under the License.\n",
    "\n",
    "#导入标准库\n",
    "import sys\n",
    "import os\n",
    "\n",
    "#导入自定义库\n",
    "sys.path.append('../baidu_api_lib')\n",
    "from baidu_couplets import baidu_couplets_create\n",
    "from baidu_sound import baidu_word_2_sound\n",
    "\n",
    "#公开课百度AI春联写作应用的id、key、secret，必须更改为自己的api接口\n",
    "baidu_app_id = '20652704'\n",
    "baidu_api_key = 'Q7TK78cRwG7jCsGc4njYIHhi'\n",
    "baidu_api_secret = '8wunyudqeDgjQq9ZTpBjL3sIpOiMUYRG'\n",
    "\n",
    "#百度AI对联应用api地址\n",
    "baidu_request_url = 'https://aip.baidubce.com/rpc/2.0/creation/v1/couplets'\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    try:\n",
    "        #传入百度AI的参数给智能春联创作\n",
    "        couplets = baidu_couplets_create(baidu_app_id, baidu_api_key, baidu_api_secret)\n",
    "        \n",
    "        #传入百度AI的参数给语音合成模块\n",
    "        word_2_sound = baidu_word_2_sound(baidu_app_id, baidu_api_key, baidu_api_secret)\n",
    "        \n",
    "        #获取春天为关键字的春联\n",
    "        word = couplets.get_couplets(baidu_request_url, \"九州\")\n",
    "        \n",
    "        #打印获取的春联\n",
    "        print(word.json()['couplets']['first'])\n",
    "        print(word.json()['couplets']['second'])\n",
    "        print(word.json()['couplets']['center'])\n",
    "        \n",
    "        #进行语音合成上联\n",
    "        word_2_sound.trans_word_to_sound(\"上联是\" + word.json()['couplets']['first'],'tst_sound.wav')\n",
    "        os.system('mplayer ' + 'tst_sound.wav')\n",
    "        \n",
    "        #进行语音合成下联\n",
    "        word_2_sound.trans_word_to_sound(\"下联是\" + word.json()['couplets']['second'],'tst_sound.wav')\n",
    "        os.system('mplayer ' + 'tst_sound.wav')\n",
    "        \n",
    "        #进行语音合成横批\n",
    "        word_2_sound.trans_word_to_sound(\"横批是\" + word.json()['couplets']['center'],'tst_sound.wav')\n",
    "        os.system('mplayer ' + 'tst_sound.wav')\n",
    "        \n",
    "        #删除临时文件    \n",
    "        os.remove('tst_sound.wav')\n",
    "                       \n",
    "    except KeyboardInterrupt: \n",
    "        os.remove('tst_sound.wav')\n",
    "        print(\"任务被终止\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
